package com.lz.webflux.project.test;

import org.springframework.scheduling.annotation.Async;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author Administrator
 */
public class AsyncExample {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("Start");


        //异步调用
        //CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> longRunningTask());
        //继续执行其他操作
        System.out.println("CompletableFuture 继续执行其他操作...");

        //获取异步任务的结果（会阻塞，直到任务完成）
        //String s = future.get();
        //System.out.println(s);
        //future.thenAccept(result -> System.out.println("CompletableFuture 任务完成: " + result));

        System.out.println("End");
        Thread.sleep(10000);
    }

    public static String longRunningTask() {
        try {
            //模拟长时间运行的任务
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Task Completed";
    }


}
